import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../main.dart';
import '../mc_router.dart';
import '../value/font_size.dart';
import '../common/MMKVTags.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<StatefulWidget> createState() => _StateMinePage();
}

class _StateMinePage extends State<MinePage> {
  void _showConfirmDialog(BuildContext context) {
    showDialog(
        context: context, // 将当前上下文传递给 showDialog
        builder: (context) {
          return AlertDialog(
            title: Text('确认操作'),
            content: Text('您确定要执行此操作吗？'),
            actions: <Widget>[
              ElevatedButton(
                child: Text('取消'),
                onPressed: () {
                  Navigator.of(context).pop(); // 关闭确认框
                },
              ),
              ElevatedButton(
                child: Text('确认'),
                onPressed: () {
                  _goLoginPage();
                  // Navigator.of(context).pop(); // 关闭确认框
                },
              ),
            ],
          );
        });
  }

  void _goLoginPage() {
    Get.toNamed(MCRouter.loginPage, preventDuplicates: true);
    print('#### url: ${MCRouter.loginPage}');
  }

  void _onLogout(BuildContext context) {
    _showConfirmDialog(context);
  }

  void _showToast() {
    print('_showToast');
    Fluttertoast.showToast(
        msg: 'This is Center Short Toast',
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.max,
            children: [
              Text("我的"),
              // 登录按钮
              Container(
                margin: const EdgeInsets.only(left: 20, right: 20, top: 20).r,
                width: double.infinity,
                child: SizedBox(
                  width: double.infinity,
                  height: 48.w,
                  child: ElevatedButton(
                    onPressed: () => _onLogout(context),
                    style: ButtonStyle(
                      backgroundColor:
                          MaterialStateProperty.all(const Color(0xffff4400)),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(24.w))),
                    ),
                    child: Text(
                      '退出登录',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: FontSize.fontSizeSmall,
                        fontWeight: FontWeight.normal,
                        decoration: TextDecoration.none,
                      ),
                    ),
                  ),
                ),
              ),
              TextButton(onPressed: _goLoginPage, child: Text('跳转到登录页面')),
              TextButton(onPressed: _showToast, child: Text('显示toast'))
            ]),
      ),
    );
  }
}
